package cn.hp.controller;

import cn.hp.entity.User;
import cn.hp.service.IUserService;
import cn.hp.util.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private IUserService userService;

        @RequestMapping("main")
        public String main() {
            return "main";
        }
        @RequestMapping("logout")
        public String error(HttpSession session){
            session.removeAttribute("user");
            return "login";
        }
        //获取登录用户信息
        @GetMapping("getUser")
        @ResponseBody
        public Result getUser(HttpSession session){
            User user = (User) session.getAttribute("user");
            return Result.success(userService.selectUserById(user.getId()));
        }

        //登录
        @PostMapping("dologin")
        @ResponseBody
        public Result dologin(@RequestBody User user,HttpSession session) {
            User user1 = userService.dologin(user);
            if(user1!=null){
                session.setAttribute("user", user1);
                return Result.success();
            }else{
                return Result.error("账号密码错误！");
            }
        }
}

